//
// Created by tacom on 22-9-7.
//

#include "CTBD.h"


CTBD::CTBD_LIST
CTBD::CTBDPredict(cv::Mat &bigMatrix, CTBD::USABLE_MATRIX &matrixShape,
                  int height, int width, int stride, intersection &sectionPredictor,
                  CTBD::FILE_NAME_LIST &fileNameList, CTBD::FILE_PATH &refineSavePath,
                  CTBD::FILE_PATH &segSavePath) {

    cv::Mat grayBigMatrix;
    cv::cvtColor(bigMatrix, grayBigMatrix, cv::COLOR_BGR2GRAY);
    CTBD_LIST ctbdList;

    try{
        for(int i = 0; i < matrixShape.size(); ++i){
            for(int j = 0; j < matrixShape[0].size(); ++j){
                if(matrixShape[i][j] == 0) continue;

                int hStart = i * (height + stride);
                int wStart = j * (width + stride);
                cv::Range h(hStart, hStart + height);
                cv::Range w(wStart, wStart + width);

                cv::Mat img1(grayBigMatrix, h, w);
                cv::Mat label1;
                int num_labels = cv::connectedComponents(img1, label1);

                unsigned int name_index = i * matrixShape[0].size() + j;
                cv::Mat img11 = cv::imread(this->pathCombine(refineSavePath, fileNameList[name_index]),cv::IMREAD_GRAYSCALE);
                intersection::INTERSECTIONS img111 = sectionPredictor.GetSkeletonIntersection(img11);

                cv::Mat imgss = cv::imread(this->pathCombine(segSavePath, fileNameList[name_index]), cv::IMREAD_GRAYSCALE);
                cv::Mat label2;
                int num__labels = cv::connectedComponents(imgss, label2);
                int NUMBER = num__labels - num_labels - img111.size();
                ctbdList.push_back(NUMBER);
            }
        }
    }catch (const std::exception &e){
        std::cout << e.what() << std::endl;
        return CTBD::CTBD_LIST();
    }

    return ctbdList;
}

std::string CTBD::pathCombine(const std::string &path, const std::string &fileName) {
    // 请务必传入合法路径，懒得写了

    if(path.at(path.size() - 1) == '/'){
        std::string absPath = path + fileName;
        return absPath;
    }else if(path.substr(path.size() - 2, 2) == "\\\\"){
        std::string absPath = path + fileName;
        return absPath;
    }else{
        return path + '/' + fileName;
    }
}

//int main(){
//    CTBD c;
//    std::string input_dir = "/home/tacom/code/Segmentation/web/back/tmp/multi_batch_tmp/tacom/12345/enhance/1";
//    std::string refine = "/home/tacom/code/Segmentation/web/back/seg_test/test_seg_cuda/test_cv_cuda/test_vascular/asserts/generate/CNBD/refine";
//    std::string seg = "/home/tacom/code/Segmentation/web/back/seg_test/test_seg_cuda/test_cv_cuda/test_vascular/asserts/generate/CNBD/seg";
//
//    // 建立名字列表
//    CTBD::FILE_NAME_LIST nameList = {"batch_1_1.png", "batch_1_2.png", "batch_1_3.png", "batch_1_4.png"};
//
//    cv::Mat l1, l2, bigImg;
//    std::vector<cv::Mat> v1, v2, v3;
//    v1 = {cv::imread(c.pathCombine(input_dir, nameList[0])),
//          cv::imread(c.pathCombine(input_dir, nameList[1]))};
//    v2 = {cv::imread(c.pathCombine(input_dir, nameList[2])),
//          cv::imread(c.pathCombine(input_dir, nameList[3]))};
//    cv::vconcat(v1, l1);
//    cv::vconcat(v2, l2);
//    v3 = {l1, l2};
//
//    // 建立大矩阵
//    cv::hconcat(v3, bigImg);
//
//    // 建立矩阵形状
//    CTBD::USABLE_MATRIX matrixShape = {{1, 1}, {1, 1}};
//
//    intersection i;
//    CTBD::CTBD_LIST result = c.CTBDPredict(bigImg, matrixShape, 384, 384, 0, i, nameList, refine, seg);
//    for(int tmp : result){
//        std::cout << "each ctbd is: " << tmp << std::endl;
//    }
//
//}

